package com.gt.wxpay;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSAExample {
    private static String privateKeyStr = "//+MOgIl0Hu+++DU/+80rAiYwmJY4Dee+/Yqf+/++//++++Gnrvccy+MI/RfgJD//wzY+7D+++6YIu3Bduc2AI+C7ItTAoGBAJdAA+tf+YDsGnuIsMixviUefNfG/iRQlGCbCRV8170s6cUy/nqhEjDnSHl0ScrdpFruDbfpNc+51TGeuaPBWeLEfLjq3Wia7yICiKir3kV3ShbnPL9DNgj6MN4ErNCqdHAriYyulJV5KdNZWksGUQ4UFmigCvv8I1MMgW7LhU4xAoGBAJjn9xdEyQJbp++932rA6A9dUubky1egKZeJUU9KjqyaR3QpPHFkA6nNGjEuuETM/6/v1hB+SfQbEpEw8hH5+j1GnqVXYkNCqpCJfRZBifhZ0dQ5uqEuGmVxRROpNL8ahy1aUcS6bXkUI1+55YNqDvXUHrYzc4PrK8tUSbVwBRdK";
    private static String publicKeyStr = "++XjgpmGrohOocK4b+++kEUT6c5A++++/ImY95n9+/";


    public static void main(String[] args) throws Exception {
//        RSAPublicKey publicKey = (RSAPublicKey) KeyFactory.getInstance("RSA")
//                .generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyStr)));
//        // 加密
//        Cipher cipher = Cipher.getInstance("RSA");
//        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//        byte[] encrypted = cipher.doFinal("Hello, World!".getBytes());
//
//        // 解密
//        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
//                .generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyStr)));
//        cipher.init(Cipher.DECRYPT_MODE, priKey);
//        byte[] decrypted = cipher.doFinal(encrypted);
//
//        // 输出结果
//        System.out.println("Original: Hello, World!");
//        System.out.println("Encrypted: " + Base64.getEncoder().encodeToString(encrypted));
//        System.out.println("Decrypted: " + new String(decrypted));

        String str = "oQgcS5nhOcbcAYgjUOImpO0ZBkCE";
        String strByRSAEncrypted = getStrByRSAEncrypted(str);
        System.out.println(strByRSAEncrypted);
        String strByRSADecrypted = getStrByRSADecrypted(strByRSAEncrypted);
        System.out.println(strByRSADecrypted);
    }

    public static String getStrByRSAEncrypted(String encodeString) throws Exception {
        RSAPublicKey publicKey = (RSAPublicKey) KeyFactory.getInstance("RSA")
                .generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyStr)));
        // 加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encrypted = cipher.doFinal(encodeString.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String getStrByRSADecrypted(String encodeString) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
                .generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyStr)));
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encodeString));
        return new String(decrypted, StandardCharsets.UTF_8);
    }
}